package com.ajk.server.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.PartMapper;
import com.ajk.server.dao.ReportMapper;
import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.Report;
import com.ajk.server.model.ReportItem;
import com.ajk.server.model.ReportPart;
import com.ajk.server.model.response.UserRes;
import com.ajk.server.service.PartService;
import com.ajk.server.service.ReportService;
import com.ajk.server.service.UsersService;
import com.ajk.server.utils.JSON;
import com.ajk.server.utils.StringUtil;

@Service
public class ReportServiceImpl implements ReportService{
	
	private static final Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);

	@Autowired
	private UsersService usersService;
	
	@Autowired
	private ReportMapper reportMapper;
	
	@Autowired
	private PartMapper partMapper;
	
	@Autowired
	private PartService partService;
	
	@Override
	@Transactional
	public Integer addReport(Report report) {
		logger.info("report : {}",JSON.toJSONString(report));
		if(report.getUserId() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(CollectionUtils.isEmpty(report.getReportItemList()) ) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "报告详情不能为空");
		}
		if(report.getReportType() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "报告类型不能为空");
		}
		if(report.getDoctorId() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "医生ID不能为空");
		}
		UserRes userRes = usersService.findUsersById(report.getUserId());
		if(userRes == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "ID为".concat(report.getUserId().toString()).concat("的用户不存在"));
		}
		report.setUserName(userRes.getName());
		report.setUserSex(userRes.getSex());
		String reportNo = StringUtil.generateOrderNO();
		report.setReportNo(reportNo);
		String checkItem = "呼吸系统、泌尿系统、消化系统";
		report.setCheckItem(checkItem);
		reportMapper.insertReport(report);
		Integer reportId = report.getId();
		for(ReportItem reportItem : report.getReportItemList()) {
			reportItem.setReportId(reportId);
			if(reportItem.getPartId() == null) {
				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "报告Item中partId不能为空");
			}
			ReportPart reportPart = partMapper.getPartById(reportItem.getPartId());
			if(reportPart == null) {
				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "报告Item中partId为"+reportItem.getPartId().intValue()+"不存在于系统中");
			}
			reportItem.setPartPath(reportPart.getPartImage());
			if(StringUtils.isEmpty(reportItem.getDiseaseNumber())) {
				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "报告Item中partId为"+reportItem.getPartId().intValue()+"的diseaseNumber参数不能为空");
			}
			String influenceLevel = "A";
			String diseaseNumber = reportItem.getDiseaseNumber();
			String[] disNum = diseaseNumber.split("-");
			Integer disNum0 = Integer.parseInt(disNum[0]) ,
					disNum1 = Integer.parseInt(disNum[1]) ,
					disNum2 = Integer.parseInt(disNum[2]) ,
					disNum3 = Integer.parseInt(disNum[3]) ,
					disNum4 = Integer.parseInt(disNum[4]) ,
					disNum5 = Integer.parseInt(disNum[5]) ;
			int baseNUm = 2;
			if(disNum5 >= baseNUm) {
				influenceLevel = "F";
			}else if(disNum4 >= baseNUm) {
				influenceLevel = "E";
			}else if(disNum3 >= baseNUm) {
				influenceLevel = "D";
			}else if(disNum2 >= baseNUm) {
				influenceLevel = "C";
			}else if(disNum1 >= baseNUm) {
				influenceLevel = "B";
			}else if(disNum0 >= baseNUm) {
				influenceLevel = "A";
			}
			reportItem.setInfluenceLevel(influenceLevel);
			reportMapper.insertReportItem(reportItem);
		}
		return reportId;
	}

	@Override
	public List<Report> getReportList(Report report) {
		List<Report> reportList = reportMapper.getReportList(report);
		if(CollectionUtils.isNotEmpty(reportList)) {
			
		}
		return reportList;
	}

	@Override
	public Report getReportById(Integer id) {
		Report report = reportMapper.getReportById(id);
		return report;
	}

	@Override
	public List<ReportItem> getReportItemByReportId(Integer reportId) {
		
		return reportMapper.getReportItemByReportId(reportId);
	}
}
